import {
    flow
}
from 'lodash';

const delay = (t = 0) => new Promise((resolve) => setTimeout(resolve, t));

import {
    generateId, formatTime
}
from 'utils/util.js';
import {
    auth
}
from 'utils/auth.js';

import {
    get
}
from 'utils/request.js';

import {
    config
}
from 'config.js';


var url = config();
// 获取应用实例
const app = getApp(); //  eslint-disable-line no-undef

Page({
    data: {
        motto: 'Hello World',
        userInfo: {},
        isSelect: false,
        startPoint: [0, 0],
        uploadCount: 10,


        countdown: 20,
        format: 'hh:mm:ss',
        numStyle: 'width: 48rpx; font-size: 18rpx; color: #fff; background-color: #000; text-align: center; border-radius: 4rpx; padding: 2rpx 0;',
        symbolStyle: 'font-size: 18rpx; color: #000; padding: 0 12rpx;',
        computeTime: 5,
        isEnd: false,
        popup: {
            status: false
        },

        dir: 'left',
        main: 'left',
        wrap: 'nowrap',
        cross: 'stretch',


        swiper: {
            imgUrls: [
                require('../../images/face.png'),
                require('../../images/1.jpg'),
                require('../../images/2.jpg')
            ],
            indicatorDots: false,
            autoplay: false,
            current: 0
        },
        animationData: {},
        markers: [{
            iconPath: require('../../images/face.png'),
            id: 0,
            latitude: 23.099994,
            longitude: 113.324520,
            width: 50,
            height: 50
        }],
        polyline: [{
            points: [{
                longitude: 113.3245211,
                latitude: 23.10229
            }, {
                longitude: 113.324520,
                latitude: 23.21229
            }],
            color: "#FF0000DD",
            width: 2,
            dottedLine: true
        }],
        controls: [{
            id: 1,
            iconPath: require('../../images/face.png'),
            position: {
                left: 0,
                top: 300 - 50,
                width: 50,
                height: 50
            },
            clickable: true
        }]
    },
    hidePopup(e) {

        this.setData({
            'popup.status': false
        })
    },
    showPopup() {
        this.setData({
            'popup.status': true
        })
    },
    // touchPopup(){
    //   console.log(33333)
    //   return false;
    // // },
    // testTap(e){
    //   console.log('heheh我是测试---start');
    //   console.log(e);
    //   console.log('heheh我是测试---end')
    // },
    //countdown
    onRunCount(e) {
        let detail = e.detail
        this.setData({
            computeTime: detail.computeTime
        })
    },
    onEndCount() {
        this.setData({
            isEnd: true
        })
    },
    countAgain() {
        let countdown = this.data.countdown + 1
        if (this.data.isEnd) {
            this.setData({
                countdown: countdown,
                isEnd: false
            })
        }
    },

    //flex  dir
    setDirToLeft: function() {
        this.setData({
            dir: 'left'
        })
    },
    setDirToRight: function() {
        this.setData({
            dir: 'right'
        })
    },
    setDirToTop: function() {
        this.setData({
            dir: 'top'
        })
    },
    setDirToBottom: function() {
        this.setData({
            dir: 'bottom'
        })
    },
    // flex  main
    setMainToStart: function() {
        this.setData({
            main: 'start'
        })
    },
    setMainToEnd: function() {
        this.setData({
            main: 'end'
        })
    },
    setMainToBetween: function() {
        this.setData({
            main: 'between'
        })
    },
    setMainToCenter: function() {
        this.setData({
            main: 'center'
        })
    },
    setMainToAround: function() {
        this.setData({
            main: 'around'
        })
    },
    //flex wrap
    setWrapToNowrap: function() {
        this.setData({
            wrap: 'nowrap'
        })
    },
    setWrapToWrap: function() {
        this.setData({
            wrap: 'wrap'
        })
    },
    setWrapToReverse: function() {
        this.setData({
            wrap: 'reverse'
        })
    },
    // flex cross
    setCrossToStretch: function() {
        this.setData({
            cross: 'stretch'
        })
    },
    setCrossToStart: function() {
        this.setData({
            cross: 'start'
        })
    },
    setCrossToEnd: function() {
        this.setData({
            cross: 'end'
        })
    },
    setCrossToCenter: function() {
        this.setData({
            cross: 'center'
        })
    },
    setCrossToBaseline: function() {
        this.setData({
            cross: 'baseline'
        })
    },
    ////异常流
    onAbnorTap() {
        wx.showToast({
            title: 'success',
            duration: 2000
        });
    },
    regionchange(e) {
        console.log(e.type)
    },
    markertap(e) {
        console.log(e.markerId)
    },
    controltap(e) {
        console.log(e.controlId)
    },
    prevImg() {

        var swiper = this.data.swiper;
        var current = swiper.current;

        console.log('previmg')
        console.log(current)
        console.log('previmg')

        swiper.current = current > 0 ? current - 1 : swiper.imgUrls.length - 1;


        this.setData({
            swiper: swiper
        })
    },
    nextImg() {
        var swiper = this.data.swiper;
        var current = swiper.current;

        console.log('nextImg')
        console.log(current)
        console.log('nextImg')

        current = current < (swiper.imgUrls.length - 1) ? current + 1 : 0;

        this.setData({
            'swiper.current': current
        })
    },
    mytouchstart(e) {
        console.log(e);
        this.setData({
            startPoint: [e.touches[0].pageX, e.touches[0].pageY]
        })
    },
    mytouchmove(e) {
        var curPoint = [e.touches[0].pageX, e.touches[0].pageY];
        var startPoint = this.data.startPoint;
        if (curPoint[0] <= startPoint[0]) {
            if (Math.abs(curPoint[0] - startPoint[0]) >= Math.abs(curPoint[1] - startPoint[1])) {
                console.log(e.timeStamp + '-touch left move');
            } else {
                if (curPoint[1] >= startPoint[1]) {
                    console.log(e.timeStamp + '-touch down move');
                } else {
                    console.log(e.timeStamp + '-touch up move');
                }
            }
        } else {
            if (Math.abs(curPoint[0] - startPoint[0]) >= Math.abs(curPoint[1] - startPoint[1])) {
                console.log(e.timeStamp + '-touch right move');
            } else {
                if (curPoint[1] >= startPoint[1]) {
                    console.log(e.timeStamp + '-touch down move');
                } else {
                    console.log(e.timeStamp + '-touch up move');
                }
            }
        }
    },
    // 事件处理函数
    bindViewTap() {
        wx.navigateTo({
            url: '../logs/logs',
        });
    },
    testTap() {
        app.wiiEvent.emit('tab', 'sdfsdfsdfsdf');
    },
    async onLoad() {

        app.wiiEvent.on('tab', tab => {
            console.log('子级' + tab);
        });

        // 调用应用实例的方法获取全局数据
        app.getUserInfo((userInfo) => {
            // 更新数据
            this.setData({
                userInfo
            });
        });

        console.log('我打印一个userinfo')
            // 调用应用实例的方法获取全局数据
        app.getUserInfo((userInfo) => {

            console.log(userInfo)

            // 更新数据
            this.setData({
                userInfo
            });
        });
        console.log('我打印一个userinfo')


        console.log('我打印一个随机值');

        // console.log(formatTime(new Date()));
        var next = function() {
            var s = generateId();
            console.log(s);
        }

        auth(next);

        get({
            'path': url.paySignCgi
        }, {}).then(function(res) {
            var cardInfoData = res.data.data;
        }, function(res) {
            console.log('失败了呵呵呵呵')
        })

        console.log('我打印一个随机值');

        await delay();

        const log = flow(() => {
            console.log('is wechat mini program: ', __WECHAT__);
            console.log('DEV: ', __DEV__);
        });

        log();

    },
    rateCallback(e) {
        console.log('rate 表示呵呵')
    },
    uploadCallback(e) {
        console.log('upload 表示呵呵')
        console.log(e)
    }
});
